/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Simulator;

/**
 * The Debug class will be called from the Control class if the Debug flag is ON for a line of code.
 * @author khhill
 */
public class Debug {
    
    /**
     * Prints the debugging information from the simulator before simulation
     * <p>
     * <b>Change Log:</b><br>
     * May 21 - Initial Code - Peter<br>
     * <p>
     * <b>Error Conditions Tested:</b><br>
     * none
     * <p>
     * <b>Date of module being installed:</b> 5/21/08
     * <p><b>Original Author:</b>  Peter Dietz
     */
    public void beforeSimulation(){
        IceAssembly.Convert convert_instance = new IceAssembly.Convert();
        
        String before_statement = "--\\/-- Before Simulation --\\/--   ";
        for(int i=0; i<4; i++){
            System.out.print(before_statement);
        }System.out.println();
        
        int lc = Control.location_counter;
        String mem_at_lc_binary = String.valueOf(Integer.parseInt(Control.memory[lc],16));
        mem_at_lc_binary = convert_instance.getBitCode(mem_at_lc_binary, 16);
        
        String op_code_binary = mem_at_lc_binary.substring(0, 2);
        String function_binary = mem_at_lc_binary.substring(2, 5);
        String s_binary = mem_at_lc_binary.substring(6);
        
        System.out.println("LC="+lc+"       MEM("+lc+")="+mem_at_lc_binary+"     op-code="+op_code_binary+"  function="+function_binary+"  s="+s_binary);
        
        int s_int = Integer.valueOf(s_binary);
        String s_mem = Control.memory[s_int];
        
        System.out.println("-->values:  S="+s_int+"  MEM("+s_int+")="+s_mem);
        System.out.println("   values:  top of data-stack="+Stack.data_stack[0]);
        System.out.println("   values:  top of test-stack="+Stack.test_stack[0]);
        
        
        before_statement = "--/\\-- Before Simulation --/\\--   ";
        for(int i=0; i<4; i++){
            System.out.print(before_statement);
        }System.out.println();System.out.println();
    }
    
    /**
     * Prints the debugging information from the simulator after simulation
     * <p>
     * <b>Change Log:</b><br>
     * May 21 - Initial Code - Peter<br>
     * <p>
     * <b>Error Conditions Tested:</b><br>
     * none
     * <p>
     * <b>Date of module being installed:</b> 5/21/08
     * <p><b>Original Author:</b>  Peter Dietz
     */
    public void afterSimulation(){
        IceAssembly.Convert convert_instance = new IceAssembly.Convert();
        
        String after_statement = "--\\/-- After Simulation --\\/--   ";
        for(int i=0; i<4; i++){
            System.out.print(after_statement);
        }System.out.println();
        
        
        int lc = Control.location_counter;
        String mem_at_lc_binary = String.valueOf(Integer.parseInt(Control.memory[lc],16));
        mem_at_lc_binary = convert_instance.getBitCode(mem_at_lc_binary, 16);
        String s_binary = mem_at_lc_binary.substring(6);
        int s_int = Integer.valueOf(s_binary);
        String s_mem = Control.memory[s_int];
        
        System.out.println("   values:   S="+s_int+"  MEM("+s_int+")="+s_mem);
        System.out.println("   values:   top of data-stack="+Stack.data_stack[0]);
        System.out.println("   values:   top of test-stack="+Stack.test_stack[0]);
        String status_binary = convert_instance.getBitCode(String.valueOf(Integer.parseInt(Stack.status_stack[0],16)),16);
        System.out.println("   values:   top of status-stack="+status_binary.substring(0, 6)+" "+status_binary.substring(6));
        System.out.println();
        System.out.println("Next LC="+Control.location_counter);
        
        
        after_statement = "--/\\-- After Simulation --/\\--   ";
        for(int i=0; i<4; i++){
            System.out.print(after_statement);
        }System.out.println();System.out.println();
    }
}
